using System;
using System.Collections.Generic;
using System.Text;

namespace SpaceWolf.GameLogic
{
    public class stock_item :ICargo
    {
        public string name;
        public int current_quantity;
        public float current_price;
        public int base_price;
        public int eco_adjust;
        public int base_quantity;
        public int mask;
        public Weight units;

        public stock_item(string name, int current_quantity,
        int current_price,
        int base_price,
        int eco_adjust,
        int base_quantity,
        int mask,
        Weight units)
        {
            this.name = name;
            this.current_quantity = current_quantity;
            this.current_price = current_price;
            this.base_price = base_price;
            this.eco_adjust = eco_adjust;
            this.base_quantity = base_quantity;
            this.mask = mask;
            this.units = units;
        }

        public void generatePriceAndQuantity(Galaxy galaxy,Commander cmdr)
        {
            int quant;
            float price;
            int market_rnd = new Random().Next(40);

            price = this.base_price;								/* Start with the base price	*/
            price += market_rnd & this.mask;					/* Add in a random amount		*/
            price += (float)Convert.ToDouble(galaxy.arPlanets[cmdr.planet].TechData.economy) * this.eco_adjust;	/* Adjust for planet economy	*/
            price /= 10;														/* Only need bottom 8 bits		*/

            quant = this.base_quantity;								/* Start with the base quantity */
            quant += this.mask;					                    /* Add in a random amount		*/
            quant -= Convert.ToInt32(galaxy.arPlanets[cmdr.planet].TechData.economy) * this.eco_adjust;	/* Adjust for planet economy	*/
            quant &= 0xFF;														/* Only need bottom 8 bits		*/

            if (quant > 127)	/* In an 8-bit environment '>127' would be negative */
                quant = 0;		/* So we set it to a minimum of zero. */

            quant &= 63;		/* Quantities range from 0..63 */

            this.current_price = price * 4;
            this.current_quantity = quant;
        }
    };


    public class Trade
    {
        public int ALIEN_ITEMS_IDX = 16;

        public List<ICargo> stock_market = new List<ICargo>();

        Commander cmdr;
        Galaxy galaxy;

        public Planet CurrentPlanet
        {
            get
            {
                return this.galaxy.arPlanets[cmdr.planet];
            }
        }

        public Trade(Commander cmdr, Galaxy galaxy)
        {
            this.cmdr = cmdr;
            this.galaxy = galaxy;

            SetNewMarket();
            generate_stock_market();
        }

        private void SetNewMarket()
        {
            // set a new market
            stock_market.Add(new stock_item("Food", 0, 0, 19, -2, 6, 0x01, Weight.t));
            stock_market.Add(new stock_item("Textiles", 0, 0, 20, -1, 10, 0x03, Weight.t));
            stock_market.Add(new stock_item("Radioactives", 0, 0, 65, -3, 2, 0x07, Weight.t));
            stock_market.Add(new stock_item("Slaves", 0, 0, 40, -5, 226, 0x1F, Weight.t));
            stock_market.Add(new stock_item("Liquor/Wines", 0, 0, 83, -5, 251, 0x0F, Weight.t));
            stock_market.Add(new stock_item("Luxuries", 0, 0, 196, 8, 54, 0x03, Weight.t));
            stock_market.Add(new stock_item("Narcotics", 0, 0, 235, 29, 8, 0x78, Weight.t));
            stock_market.Add(new stock_item("Computers", 0, 0, 154, 14, 56, 0x03, Weight.t));
            stock_market.Add(new stock_item("Machinery", 0, 0, 117, 6, 40, 0x07, Weight.t));
            stock_market.Add(new stock_item("Alloys", 0, 0, 78, 1, 17, 0x1F, Weight.t));
            stock_market.Add(new stock_item("Firearms", 0, 0, 124, 13, 29, 0x07, Weight.t));
            stock_market.Add(new stock_item("Furs", 0, 0, 176, -9, 220, 0x3F, Weight.t));
            stock_market.Add(new stock_item("Minerals", 0, 0, 32, -1, 53, 0x03, Weight.t));
            stock_market.Add(new stock_item("Gold", 0, 0, 97, -1, 66, 0x07, Weight.kg));
            stock_market.Add(new stock_item("Platinum", 0, 0, 171, -2, 55, 0x1F, Weight.kg));
            stock_market.Add(new stock_item("Gem-Stones", 0, 0, 45, -1, 250, 0x0F, Weight.g));
            stock_market.Add(new stock_item("Alien Items", 0, 0, 53, 15, 192, 0x07, Weight.t));

            SetMilitaryMarket();
        }

        public void generate_stock_market()
        {


            for (int iCount = 0; iCount < stock_market.Count; iCount++)
            {
              stock_market[iCount].generatePriceAndQuantity(galaxy,cmdr);
            }
            /* Alien Items are never available for purchase... */

            //stock_market[ALIEN_ITEMS_IDX].current_quantity = 0;
        }

        // Military
        public List<IEquipment> Equipment = new List<IEquipment>();

        void SetMilitaryMarket()
        {
            Equipment.Add(Armory.PulseLaser);
            Equipment.Add(Armory.BeamLaser);
            Equipment.Add(Armory.MiningLaser);
            Equipment.Add(Armory.MilitaryLaser);
            Equipment.Add(Armory.LightLaser);

            Equipment.Add(Armory.Coolant1);
            Equipment.Add(Armory.Coolant2);
            Equipment.Add(Armory.Coolant3);
            Equipment.Add(Armory.Coolant4);
        }//SetMilitaryMarket()
    }
}
